package com.app.milkteaapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.adapter.CommentAdapter;
import com.app.milkteaapp.bean.Comment;
import com.app.milkteaapp.bean.FindCommentGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.bean.FindUserGsonBean;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.utils.OkHttpUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView listviewComment;
    private Button btnAddcomment;
    private Intent intent;
    private int milkTeaId;
    private Context context = DetailsActivity.this;
    CommentAdapter commentAdapter;
    private int userid;
    private List<FindCommentGsonBean.Comment> comments;
    List<Comment> commentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        initView();
        sendRequest();
    }

    private void initView() {
        listviewComment = (ListView) findViewById(R.id.listview_comment);
        btnAddcomment = (Button) findViewById(R.id.btn_addcomment);
        this.intent = getIntent();
        Intent intent = this.intent;
        milkTeaId = intent.getIntExtra("id", -1);

        btnAddcomment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_addcomment:
                    Intent intent = new Intent(getApplicationContext(),AddCommentActivity.class);
                    intent.putExtra("teaid",milkTeaId);
                    startActivity(intent);
                break;
        }
    }

    /**
     * 发送请求
     */
    public void sendRequest() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",milkTeaId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Call call = OkHttpUtils.MyCall(MyTools.URL + "/comment/findById", jsonObject.toString());
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //gson解析
                String string = response.body().string();
                FindCommentGsonBean findCommentGsonBean = GsonUtils.fromJson(string, FindCommentGsonBean.class);
                comments = findCommentGsonBean.getData();
                //请求成功后更新评论列表
                if (findCommentGsonBean.isSuccess()) {

                    for (int i = 0; i< comments.size(); i++){
                        userid = comments.get(i).getUserid();
                        nameByUserId(userid,i);
                    }

                    updateUi(commentList);
                }
            }
        });
    }

    private void nameByUserId(int id, final int i) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.MyCall(MyTools.URL+"/user/findById",jsonObject.toString()).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                FindUserGsonBean findUserGsonBean = GsonUtils.fromJson(string,FindUserGsonBean.class);
                if(findUserGsonBean.isSuccess()){
                    Comment comment = new Comment();
                    comment.setUserName(findUserGsonBean.getData().getName());
                    comment.setDate(comments.get(i).getDate());
                    comment.setScore(comments.get(i).getScore());
                    comment.setComment(comments.get(i).getComment());
                    commentList.add(comment);
                }
            }
        });
    }

    //更新评论列表
    public void updateUi(final List<Comment> data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                commentAdapter = new CommentAdapter(context,data);
                listviewComment.setAdapter(commentAdapter);

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        sendRequest();
    }
}
